//
//  MCChaseNumberViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/16.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCChaseNumberViewController.h"
#import "MCZhuihaoTableViewCell.h"
#import "MCPickBottomButton.h"
#import "MCZhuiHaoModel.h"
#import "MCPaySelectedLotteryHeaderView.h"
#import "MCBetModel.h"
#import "MCIssueModel.h"
#import "MCMaxbonusModel.h"
#import "MCPickNumberViewController.h"
#import "MCMainTabBarController.h"
#import "MCGameRecordViewController.h"
#import "MCUserMoneyModel.h"

@interface MCChaseNumberViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

#pragma mark - property

/**segmentCotrol*/
@property(nonatomic,weak) UISegmentedControl *segmentCotrol;

/**下部滑动根view*/
@property(nonatomic,weak) UIScrollView *baseScrollView;

/**segmentCotrol下面的输入框和按钮的父控件*/
@property(nonatomic,weak) UIView *topFilterView;

/**数据源*/
@property(nonatomic,strong) NSMutableArray *selectedCheckBoxArray;

/**baseScrollView上的左边的tableview*/
@property (nonatomic,weak) UITableView *leftTableView;

/**baseScrollView上的中间的tableview*/
@property (nonatomic,weak) UITableView *middleTableView;

/**baseScrollView上的右边的tableview*/
@property (nonatomic,weak) UITableView *rightTableView;

/**当前cell的frame*/
@property (nonatomic,assign) CGRect currentRect;

/**底部view*/
@property (nonatomic,weak) UIView *bottomInfoView;

/**底部view中的著述*/
@property (nonatomic,weak) UILabel *middleZHUSHULabel;

/**底部view余额*/
@property (nonatomic,weak) UILabel *yuELabel;

/**被选中的cell*/
@property (nonatomic,strong) NSMutableArray *selectedArray;

/**倒计时*/
@property (nonatomic,weak) UIView *timerView;

/**同倍起始输入框*/
@property (nonatomic,weak) UITextField *startTF;

/**同倍追号输入框*/
@property (nonatomic,weak) UITextField *zhuiTF;

/**翻倍起始输入框*/
@property (nonatomic,weak) UITextField *startFanBeiTF;

/**翻倍追号输入框*/
@property (nonatomic,weak) UITextField *zhuiFanBeiTF;

/**翻倍隔输入框*/

@property (nonatomic,weak) UITextField *geFanBeiTF;

/**翻倍倍输入框*/
@property (nonatomic,weak) UITextField *beiFanBeiTF;

/**利润率起始输入框*/
@property (nonatomic,weak) UITextField *liRunStartTF;

/**利润率追号输入框*/
@property (nonatomic,weak) UITextField *liRunZhuiTF;

/**利润率输入框*/
@property (nonatomic,weak) UITextField *liRunTF;

/**模型强引用*/
//追号页面-获取固定个数的期号
@property (nonatomic,strong) MCZhuiHaoModel *chaseIssuesModel;

@property (nonatomic,strong) MCIssueModel *issueModel;

@property (nonatomic,strong) MCBetModel *betModel;

/**消费金额*/
@property (nonatomic,assign) float needMoney;

@property (nonatomic,weak) UIView *topBtnView;

@property (nonatomic,weak) UIButton *lastBtn;

@property (nonatomic,weak) UIView *botView;

@property (nonatomic,assign) long count;

@property (nonatomic,assign) CGFloat singlePrice;

@property (nonatomic,assign) BOOL isStop;


@end

@implementation MCChaseNumberViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isStop= YES;
    [self setUpUI];
    self.navigationItem.title = self.dataSourceModel.czName;
    self.view.backgroundColor = [UIColor whiteColor];
    [self reloadDataAndShowWithNum:10 andTableView:self.leftTableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated ];
    [self registerNotifacation];
    [self.selectedArray removeAllObjects];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.yuEHadChangedBlock) {
        self.yuEHadChangedBlock(self.yuELabel.text);
    }
     [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)dealloc{
    
    NSLog(@"MCChaseNumberViewController -- dealloc");

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - setUpUI
- (void)setUpUI{
    [self setUpTimer];
    [self setUpSegmentControl];
    [self setUpBaseScorllView];
    [self setUpBottomView];
    [self setUpTopFilterView];
    [self setUpMiddleTabView];
    [self setDefaultValue];
}

#pragma mark-顶部倒计时
- (void)setUpTimer{
    MCPaySelectedLotteryHeaderView *timerView = [[MCPaySelectedLotteryHeaderView alloc] init];
    timerView.backgroundColor = RGB(236, 236, 236);
    [self.view addSubview:timerView];
    timerView.frame=CGRectMake(0, 0, G_SCREENWIDTH, [MCPaySelectedLotteryHeaderView computeHeight:nil]);
    timerView.IssueNumber=_IssueNumber;
    timerView.dataSource=_RemainTime;
    self.timerView = timerView;
    __weak MCPaySelectedLotteryHeaderView *weakTimerView = timerView;
    __weak typeof(self) weakSelf = self;
    timerView.timeISZeroBlock = ^{
            MCIssueModel *issueModel = [[MCIssueModel alloc] init];
            issueModel.lotteryNumber = [weakSelf.dataSourceModel.LotteryID intValue];
            [issueModel refreashDataAndShow];
            weakSelf.issueModel = issueModel;
            issueModel.callBackSuccessBlock = ^(id manager) {
                MCIssueModel *model = [MCIssueModel mj_objectWithKeyValues:manager];
                weakTimerView.IssueNumber=model.IssueNumber;
                weakTimerView.dataSource=model.RemainTime;
            };
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的追号中有已经截止的期号，请重新选择" preferredStyle:UIAlertControllerStyleAlert];
        // 添加按钮
        __weak typeof(self) weakAlert = self;
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if (weakAlert.lastBtn.tag == 1000) {
                [weakAlert tongBeiPlanBtnClick];
            } else if(weakAlert.lastBtn.tag == 1001){
                [weakAlert fanBeiPlanBtnClick];
            }else{
                [weakAlert liRuanPlanBtnClick];
            }
        }]];
  
       [self presentViewController:alert animated:YES completion:nil];
    };

}

#pragma mark-条件选择
- (void)setUpSegmentControl{
    NSArray *arr = nil;
      MCPaySelectedCellModel * model = self.dataSourceModel.dataSource.firstObject;
    if (self.dataSourceModel.dataSource.count > 1 || model.isCanProfitChase == NO ) {
       arr = @[@"同倍追号",@"翻倍追号"];
    } else {
       arr =  @[@"同倍追号",@"翻倍追号",@"利润率追号"];
    }
    //条件选择
    UIView *topBtnView = [[UIView alloc] init];
    long count = arr.count;
    self.count = count;
    [self.view addSubview:topBtnView];
    self.topBtnView = topBtnView;
    topBtnView.frame = CGRectMake(0, 25, G_SCREENWIDTH, MC_REALVALUE(45));
    for (NSInteger i = 0; i<arr.count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [topBtnView addSubview:btn];
        btn.frame = CGRectMake(G_SCREENWIDTH / count * i, 0, G_SCREENWIDTH / count, 42);
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitleColor:RGB(54, 128, 211) forState:UIControlStateSelected];
        [btn setTitleColor:RGB(102, 102, 102) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14.0f)];
        btn.tag = i + 1000;
        [btn addTarget:self action:@selector(segmentContrlBtnClick:) forControlEvents:UIControlEventTouchDown];
    }
    
    UIView *botView = [[UIView alloc] init];
    self.botView = botView;
    botView.frame = CGRectMake(0, 42, G_SCREENWIDTH / count, 2);
    botView.backgroundColor = RGB(54, 128, 211);
    [topBtnView addSubview:botView];
    UIView *botView1 = [[UIView alloc] init];
    botView1.frame = CGRectMake(0, 44, G_SCREENWIDTH, 0.5);
    botView1.backgroundColor = RGB(68, 68, 68);
    [topBtnView addSubview:botView1];
    
}

- (void)segmentContrlBtnClick:(UIButton *)btn{
    self.lastBtn.selected = NO;
    btn.selected = YES;
    self.lastBtn = btn;
    
    [UIView animateWithDuration:0.25 animations:^{
        long index = btn.tag - 1000;
        self.botView.frame = CGRectMake(index * G_SCREENWIDTH / self.count , 43, G_SCREENWIDTH / self.count, 2);
    }];
    if (btn.tag == 1000) {
        self.zhuiTF.text=@"10";
        [self tongBeiPlanBtnClick];
    } else if(btn.tag == 1001){
        self.zhuiFanBeiTF.text = @"10";
        [self fanBeiPlanBtnClick];
    }else{
        self.liRunZhuiTF.text=@"10";
        [self liRuanPlanBtnClick];
    }
    long index = btn.tag - 1000;
    [self.baseScrollView setContentOffset:CGPointMake(index * G_SCREENWIDTH, 0) animated:YES];
    
}


#pragma mark-BaseScorllView
- (void)setUpBaseScorllView{
    UIScrollView *baseScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:baseScrollView];
    [baseScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timerView.mas_bottom).offset(MC_REALVALUE(45));
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
        
    }];
    baseScrollView.scrollEnabled = NO;
    if (self.dataSourceModel.dataSource.count > 1) {
        baseScrollView.contentSize = CGSizeMake(G_SCREENWIDTH * 2, baseScrollView.heiht);
    } else {
        baseScrollView.contentSize = CGSizeMake(G_SCREENWIDTH * 3, baseScrollView.heiht);
    }
    baseScrollView.pagingEnabled = YES;
    baseScrollView.bounces = NO;
    baseScrollView.showsVerticalScrollIndicator = NO;
    baseScrollView.showsHorizontalScrollIndicator = NO;
    self.baseScrollView = baseScrollView;
    self.baseScrollView.delegate = self;
}

#pragma mark-TopFilterView
- (void)setUpTopFilterView{
    
    [self setUpLeftFilterView];
    [self setUpTopMiddleFilterView];
    [self setUpRightFilterView];
    
}
#pragma mark-同倍选择框
- (void)setUpLeftFilterView{
    //left
    UIView *topFilterView = [[UIView alloc] init];
    [self.baseScrollView addSubview:topFilterView];
    self.topFilterView = topFilterView;
    [topFilterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.baseScrollView);
        make.height.equalTo(@(110));
        make.width.equalTo(@(G_SCREENWIDTH));
    }];
    
    
    UILabel *startLabel = [[UILabel alloc] init];
    [topFilterView addSubview:startLabel];
    startLabel.text = @"起始倍数:";
    startLabel.textColor = RGB(119, 119, 119);
    startLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [startLabel sizeToFit];
    CGFloat padding = MC_REALVALUE(10);
    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topFilterView.mas_left).offset(padding);
        make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(20));
        
    }];
    
    UITextField *startTF = [[UITextField alloc] init];
    startTF.delegate = self;
    startTF.layer.cornerRadius = 4.0f;
    self.startTF = startTF;
    startTF.textAlignment = NSTextAlignmentCenter;
    [topFilterView addSubview:startTF];
    
    startTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    startTF.layer.borderWidth = 0.5f;
    [startTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_right).offset(MC_REALVALUE(10));
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.width.equalTo(@(MC_REALVALUE(80)));
    }];
    startTF.keyboardType = UIKeyboardTypeNumberPad;
    UITextField *zhuiTF = [[UITextField alloc] init];
    zhuiTF.delegate = self;
    [topFilterView addSubview:zhuiTF];
    zhuiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiTF.layer.borderWidth = 0.5f;
 
    zhuiTF.textAlignment = NSTextAlignmentCenter;
    zhuiTF.layer.cornerRadius = 4.0f;
    UILabel *zhuiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiLabel];
    zhuiLabel.text = @"追号期数:";
    zhuiLabel.textColor = RGB(119, 119, 119);
    zhuiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_left);
        make.top.equalTo(self.startTF.mas_bottom).offset(MC_REALVALUE(20));
        
    }];
    [zhuiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(zhuiLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.left.equalTo(zhuiLabel.mas_right).offset(MC_REALVALUE(10));
        make.width.equalTo(@(MC_REALVALUE(80)));
        
    }];

    UIButton *planBtn = [[UIButton alloc] init];
    [topFilterView addSubview:planBtn];
    [planBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topFilterView.mas_right).offset(-MC_REALVALUE(35));
        make.top.equalTo(topFilterView).offset(MC_REALVALUE(32));
        make.width.equalTo(@(MC_REALVALUE(115)));
        make.height.equalTo(@(MC_REALVALUE(30)));
    }];
    UILabel *infoLabel = [[UILabel alloc] init];
    self.zhuiTF = zhuiTF;
    [topFilterView addSubview:infoLabel];
    infoLabel.text = @"(最多可追120期)";
    zhuiTF.keyboardType = UIKeyboardTypeNumberPad;
    infoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    infoLabel.textColor = MC_SKYBLUE_COLOR;
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(planBtn.mas_bottom).offset(MC_REALVALUE(2.5));
        make.centerX.equalTo(planBtn);
    }];
    [planBtn setTitle:@"生成追号计划" forState:UIControlStateNormal];
    planBtn.layer.cornerRadius = 4.0f;
    planBtn.layer.masksToBounds = YES;
    [planBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    planBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    planBtn.backgroundColor = MC_SKYBLUE_COLOR;
    [planBtn addTarget:self action:@selector(tongBeiPlanBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark-翻倍选择框
- (void)setUpTopMiddleFilterView{
    //Middle
    UIView *topFilterView = [[UIView alloc] init];
    [self.baseScrollView addSubview:topFilterView];
    [topFilterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseScrollView);
        make.height.equalTo(@(MC_REALVALUE(110)));
        make.width.equalTo(@(G_SCREENWIDTH));
        make.left.equalTo(@(G_SCREENWIDTH));
    }];
    
    UILabel *startLabel = [[UILabel alloc] init];
    [topFilterView addSubview:startLabel];
    startLabel.text = @"起始倍数:";
    startLabel.textColor = RGB(119, 119, 119);
    startLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [startLabel sizeToFit];
    CGFloat padding = MC_REALVALUE(10);
    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topFilterView.mas_left).offset(padding);
        make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(20));
        
    }];
    
    UITextField *startTF = [[UITextField alloc] init];
    [topFilterView addSubview:startTF];
    startTF.textAlignment = NSTextAlignmentCenter;
    self.startFanBeiTF = startTF;
    startTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    startTF.layer.borderWidth = 0.5f;
    startTF.keyboardType = UIKeyboardTypeNumberPad;
    [startTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_right).offset(MC_REALVALUE(10));
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.width.equalTo(@(MC_REALVALUE(80)));
    }];
    startTF.layer.cornerRadius = 4.0f;
    UILabel *beiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:beiLabel];
    beiLabel.text = @"倍";
    beiLabel.textColor = RGB(119, 119, 119);
    beiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [beiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topFilterView).offset(-MC_REALVALUE(10));
        make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(20));
        
    }];
    
    UITextField *beiTF = [[UITextField alloc] init];
    self.beiFanBeiTF = beiTF;
    beiTF.layer.cornerRadius = 4.0f;
    [topFilterView addSubview:beiTF];
    beiTF.textAlignment = NSTextAlignmentCenter;
    beiTF.keyboardType = UIKeyboardTypeNumberPad;
    beiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    beiTF.layer.borderWidth = 0.5f;
    [beiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.right.equalTo(beiLabel.mas_left).offset(-5);
        make.width.equalTo(@(MC_REALVALUE(50)));
        
    }];
        UILabel *XLabel = [[UILabel alloc] init];
        [topFilterView addSubview:XLabel];
        XLabel.text = @"X";
        XLabel.textColor = RGB(119, 119, 119);
        XLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
        [XLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(beiTF.mas_left).offset(-MC_REALVALUE(5));
             make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(20));
            
        }];

    UITextField *geTF = [[UITextField alloc] init];
    geTF.layer.cornerRadius = 4.0f;
    [topFilterView addSubview:geTF];
    self.geFanBeiTF = geTF;
    geTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    geTF.layer.borderWidth = 0.5f;
    [geTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.right.equalTo(XLabel.mas_left).offset(-MC_REALVALUE(5));
        make.width.equalTo(@(MC_REALVALUE(50)));
        
    }];
    geTF.textAlignment = NSTextAlignmentCenter;
    geTF.keyboardType = UIKeyboardTypeNumberPad;
    UILabel *geLabel = [[UILabel alloc] init];
    [topFilterView addSubview:geLabel];
    geLabel.text = @"隔";
    geLabel.textColor = RGB(119, 119, 119);
    geLabel.font = [UIFont systemFontOfSize:14];
    [geLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(geTF.mas_left).offset(-MC_REALVALUE(5));
         make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(20));
        
    }];


    
    UILabel *zhuiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiLabel];
    zhuiLabel.text = @"追号期数:";
    zhuiLabel.textColor = RGB(119, 119, 119);
    zhuiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_left);
        make.top.equalTo(startLabel.mas_bottom).offset(MC_REALVALUE(20));
        
    }];
    
    UITextField *zhuiTF = [[UITextField alloc] init];
    zhuiTF.keyboardType = UIKeyboardTypeNumberPad;
    zhuiTF.layer.cornerRadius = 4.0f;
    self.zhuiFanBeiTF = zhuiTF;
    zhuiTF.textAlignment = NSTextAlignmentCenter;
    [topFilterView addSubview:zhuiTF];
    zhuiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiTF.layer.borderWidth = 0.5f;
    [zhuiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(zhuiLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.left.equalTo(zhuiLabel.mas_right).offset(MC_REALVALUE(10));
        make.width.equalTo(@(MC_REALVALUE(80)));
        
    }];
    
  
    UIButton *planBtn = [[UIButton alloc] init];
    [topFilterView addSubview:planBtn];
    [planBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhuiTF);
        make.width.equalTo(@(MC_REALVALUE(115)));
        make.right.equalTo(topFilterView).offset(-MC_REALVALUE(35));
        make.height.equalTo(@(30));
    }];
    UILabel *infoLabel = [[UILabel alloc] init];
    [topFilterView addSubview:infoLabel];
    infoLabel.text = @"(最多可追120期)";
    infoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    infoLabel.textColor = MC_SKYBLUE_COLOR;
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(planBtn.mas_bottom).offset(2.5);
        make.centerX.equalTo(planBtn);
    }];
    [planBtn setTitle:@"生成追号计划" forState:UIControlStateNormal];
    planBtn.layer.cornerRadius = 4.0f;
    planBtn.layer.masksToBounds = YES;
    [planBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    planBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    planBtn.backgroundColor = MC_SKYBLUE_COLOR;
    [planBtn addTarget:self action:@selector(fanBeiPlanBtnClick) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark-利润率选择框
- (void)setUpRightFilterView{
    //Middle
    UIView *topFilterView = [[UIView alloc] init];
    [self.baseScrollView addSubview:topFilterView];
    [topFilterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseScrollView);
        make.height.equalTo(@(MC_REALVALUE(110)));
        make.width.equalTo(@(G_SCREENWIDTH));
        make.left.equalTo(@(G_SCREENWIDTH *2));
    }];
    
    
    UILabel *startLabel = [[UILabel alloc] init];
    [topFilterView addSubview:startLabel];
    startLabel.text = @"起始倍数:";
    startLabel.textColor = RGB(119, 119, 119);
    startLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [startLabel sizeToFit];
    CGFloat padding = MC_REALVALUE(10);
    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topFilterView.mas_left).offset(padding);
        make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(20));
        
    }];
    
    UITextField *startTF = [[UITextField alloc] init];
    startTF.textAlignment = NSTextAlignmentCenter;
    [topFilterView addSubview:startTF];
    startTF.layer.cornerRadius = 4.0f;
    startTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    startTF.layer.borderWidth = 0.5f;
    [startTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_right).offset(MC_REALVALUE(10));
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.width.equalTo(@(MC_REALVALUE(80)));
    }];
    self.liRunStartTF = startTF;
    startTF.keyboardType = UIKeyboardTypeNumberPad;
    UILabel *persentLabel = [[UILabel alloc] init];
    [topFilterView addSubview:persentLabel];
    persentLabel.text = @"%";
    persentLabel.textColor = RGB(119, 119, 119);
    persentLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [persentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topFilterView).offset(-padding);
        make.top.equalTo(startLabel);
        
    }];
    
    UITextField *zhuiDITF = [[UITextField alloc] init];
    [topFilterView addSubview:zhuiDITF];
    self.liRunTF = zhuiDITF;
    zhuiDITF.layer.cornerRadius = 4.0f;
    zhuiDITF.keyboardType = UIKeyboardTypeNumberPad;
    zhuiDITF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiDITF.layer.borderWidth = 0.5f;
    zhuiDITF.textAlignment = NSTextAlignmentCenter;
    [zhuiDITF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.right.equalTo(persentLabel.mas_left);
        make.width.equalTo(@(MC_REALVALUE(80)));
        
    }];
    UILabel *zhuiDiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiDiLabel];
    zhuiDiLabel.text = @"最低收益率:";
    zhuiDiLabel.textColor = RGB(119, 119, 119);
    zhuiDiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiDiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(zhuiDITF.mas_left).offset(-3);
        make.top.equalTo(zhuiDITF);
        make.centerY.equalTo(zhuiDITF);
    }];

    UILabel *zhuiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiLabel];
    zhuiLabel.text = @"追号期数:";
    zhuiLabel.textColor = RGB(119, 119, 119);
    zhuiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_left);
        make.top.equalTo(startLabel.mas_bottom).offset(MC_REALVALUE(20));
    }];
    
    UITextField *zhuiTF = [[UITextField alloc] init];
    zhuiTF.textAlignment = NSTextAlignmentCenter;
    zhuiTF.layer.cornerRadius = 4.0f;
    [topFilterView addSubview:zhuiTF];
    zhuiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiTF.layer.borderWidth = 0.5f;
    [zhuiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(zhuiLabel);
        make.height.equalTo(@(MC_REALVALUE(25)));
        make.left.equalTo(zhuiLabel.mas_right).offset(MC_REALVALUE(10));
        make.width.equalTo(@(MC_REALVALUE(80)));
        
    }];
    self.liRunZhuiTF = zhuiTF;
    zhuiTF.keyboardType = UIKeyboardTypeNumberPad;
    UILabel *infoLabel = [[UILabel alloc] init];
    [topFilterView addSubview:infoLabel];
    infoLabel.text = @"(最多可追120期)";
    infoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    infoLabel.textColor = MC_SKYBLUE_COLOR;
  
    UIButton *planBtn = [[UIButton alloc] init];
    [topFilterView addSubview:planBtn];
    [planBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhuiTF);
        make.width.equalTo(@(MC_REALVALUE(115)));
        make.right.equalTo(topFilterView).offset(-MC_REALVALUE(35));
        make.height.equalTo(@30);
    }];
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(planBtn.mas_bottom).offset(2.5);
        make.centerX.equalTo(planBtn);
    }];
    [planBtn setTitle:@"生成追号计划" forState:UIControlStateNormal];
    planBtn.layer.cornerRadius = 4.0f;
    planBtn.layer.masksToBounds = YES;
    [planBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    planBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    planBtn.backgroundColor = MC_SKYBLUE_COLOR;
    [planBtn addTarget:self action:@selector(liRuanPlanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

/**
 *
 *MiddleTabView
 *
 **/
- (void)setUpMiddleTabView{
    
    [self setUpLeftMiddleView];
    [self setUpMiddleMiddeleView];
    [self setUPMiddleRightView];
}
#pragma mark-同倍TabView
- (void)setUpLeftMiddleView{
    UIView *middleView = [[UIView alloc] init];
    [self.baseScrollView addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topFilterView.mas_bottom);
        make.left.equalTo(self.topFilterView.mas_left);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    UIView *lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = RGB(119, 119, 119);
    [middleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(middleView);
        make.height.equalTo(@(0.5));
    }];
    NSArray *arr = @[@"序号",@"期数",@"倍数",@"金额"];
    for (NSInteger i = 0; i< 4; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        [middleView addSubview:label];
        label.textColor = MC_SKYBLUE_COLOR;
        label.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
        label.text = arr[i];
        label.textAlignment = NSTextAlignmentCenter;
        if (i == 0) {
            label.frame = CGRectMake(0, MC_REALVALUE(5), (G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(20));
        } else if(i == 1){
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(5), 150, MC_REALVALUE(20));
        }else if(i == 2 ){
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5 + 150 , MC_REALVALUE(5), 68, MC_REALVALUE(20));
        }else{
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5 + 150 + 68 , MC_REALVALUE(5), (G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(20));
        }
    }
    UITableView *middleTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    middleTabView.estimatedRowHeight = 2;
    [middleView addSubview:middleTabView];
    [middleTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(31)));
        make.left.equalTo(self.view.mas_left);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    middleTabView.delegate = self;
    middleTabView.dataSource = self;
    middleTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    middleTabView.rowHeight = MC_REALVALUE(33);
    [middleTabView registerNib:[UINib nibWithNibName:NSStringFromClass([MCZhuihaoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"zhuihaocell"];
    self.leftTableView = middleTabView;
}

#pragma mark-翻倍TabView
- (void)setUpMiddleMiddeleView{

    UIView *middleView = [[UIView alloc] init];
    [self.baseScrollView addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topFilterView.mas_bottom);
        make.left.equalTo(self.baseScrollView.mas_left).offset(G_SCREENWIDTH);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    
    UIView *lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = RGB(119, 119, 119);

    [middleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(middleView);
        make.height.equalTo(@(0.5));
    }];
    
    NSArray *arr = @[@"序号",@"期数",@"倍数",@"金额"];
    for (NSInteger i = 0; i< 4; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        [middleView addSubview:label];
        label.textColor = MC_SKYBLUE_COLOR;
        label.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
        label.text = arr[i];
        label.textAlignment = NSTextAlignmentCenter;
        if (i == 0) {
            label.frame = CGRectMake(0, MC_REALVALUE(5), (G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(20));
        } else if(i == 1){
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(5), 150, MC_REALVALUE(20));
        }else if(i == 2 ){
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5 + 150 , MC_REALVALUE(5), 68, MC_REALVALUE(20));
        }else{
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5 + 150 + 68 , MC_REALVALUE(5), (G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(20));
        }
    }
    UITableView *middleTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [middleView addSubview:middleTabView];
    [middleTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(31)));
        make.left.equalTo(middleView);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    middleTabView.delegate = self;
    middleTabView.dataSource = self;
    middleTabView.rowHeight = MC_REALVALUE(33);
    middleTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [middleTabView registerNib:[UINib nibWithNibName:NSStringFromClass([MCZhuihaoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"zhuihaocell"];
    self.middleTableView = middleTabView;
 
}

#pragma mark-利润率TabView
- (void)setUPMiddleRightView{
    
    UIView *middleView = [[UIView alloc] init];
    [self.baseScrollView addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topFilterView.mas_bottom);
        make.left.equalTo(self.baseScrollView.mas_left).offset(2*G_SCREENWIDTH);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    UIView *lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = RGB(119, 119, 119);
    [middleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(middleView);
        make.height.equalTo(@(0.5));
    }];
    NSArray *arr = @[@"序号",@"期数",@"倍数",@"金额"];
    for (NSInteger i = 0; i< 4; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        [middleView addSubview:label];
        label.textColor = MC_SKYBLUE_COLOR;
        label.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
        label.text = arr[i];
        if (i == 0) {
            label.frame = CGRectMake(0, MC_REALVALUE(5), (G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(20));
        } else if(i == 1){
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(5), 150, MC_REALVALUE(20));
        }else if(i == 2 ){
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5 + 150 , MC_REALVALUE(5), 68, MC_REALVALUE(20));
        }else{
            label.frame = CGRectMake((G_SCREENWIDTH - 218) * 0.5 + 150 + 68 , MC_REALVALUE(5), (G_SCREENWIDTH - 218) * 0.5, MC_REALVALUE(20));
        }
       
    
        label.textAlignment = NSTextAlignmentCenter;
        
    }
    UITableView *middleTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [middleView addSubview:middleTabView];
    [middleTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(31)));
        make.left.equalTo(middleView);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    middleTabView.delegate = self;
    middleTabView.dataSource = self;
    middleTabView.rowHeight = MC_REALVALUE(33);
    middleTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [middleTabView registerNib:[UINib nibWithNibName:NSStringFromClass([MCZhuihaoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"zhuihaocell"];
    self.rightTableView = middleTabView;

}
/**
 *
 *MiddleTabView
 *
 **/
#pragma mark-底部金额
- (void)setUpBottomView{
    
    UIView *bottomInfoView = [[UIView alloc] init];
    [self.view addSubview:bottomInfoView];
    [bottomInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(MC_REALVALUE(49)));
    }];
    bottomInfoView.backgroundColor = RGB(241, 241, 241);
    self.bottomInfoView = bottomInfoView;
    
    UIButton *stopBtn =  [[UIButton alloc] init];
    [bottomInfoView addSubview:stopBtn];
    stopBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    [stopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomInfoView).offset(MC_REALVALUE(10));
        make.centerY.equalTo(bottomInfoView);
        make.top.equalTo(bottomInfoView).offset(MC_REALVALUE(13));
    }];
    
    [stopBtn setTitle:@"中奖停追" forState:UIControlStateNormal];
    [stopBtn setTitleColor:RGB(119, 119, 119) forState:UIControlStateNormal];
    [stopBtn setImage:[UIImage imageNamed:@"Choice_button_Unchecked"] forState:UIControlStateNormal];
    [stopBtn setImage:[UIImage imageNamed:@"Choice_button_selected"] forState:UIControlStateSelected];
    [stopBtn addTarget:self action:@selector(stopBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [stopBtn sizeToFit];
    stopBtn.selected = YES;
    MCPickBottomButton *payBtn = [[MCPickBottomButton alloc] init];
    [bottomInfoView addSubview:payBtn];
    payBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    payBtn.backgroundColor = RGB(0, 130, 210);
    [payBtn setTitle:@"付款" forState:UIControlStateNormal];
    [payBtn setImage:[UIImage imageNamed:@"tabbar_payment_selected"] forState:UIControlStateNormal];
    [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(bottomInfoView);
        make.width.equalTo(@(MC_REALVALUE(90)));
    }];
    
    UILabel *middleZHUSHULabel = [[UILabel alloc] init];
    middleZHUSHULabel.textColor = RGB(68, 68, 68);
    self.middleZHUSHULabel = middleZHUSHULabel;
    middleZHUSHULabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    middleZHUSHULabel.textAlignment = NSTextAlignmentCenter;
    [bottomInfoView addSubview:middleZHUSHULabel];
    [middleZHUSHULabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(6)));
        make.centerX.equalTo(bottomInfoView);
        make.left.equalTo(stopBtn.mas_right);
        make.right.equalTo(payBtn.mas_left);
    }];
    
    UILabel *yuELabel = [[UILabel alloc] init];
    [bottomInfoView addSubview:yuELabel];
    self.yuELabel = yuELabel;
    yuELabel.textColor = RGB(68, 68, 68);
    yuELabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    yuELabel.textAlignment = NSTextAlignmentCenter;
    [yuELabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomInfoView);
        make.bottom.equalTo(bottomInfoView).offset(-MC_REALVALUE(6));
        make.left.equalTo(stopBtn.mas_right);
        make.right.equalTo(payBtn.mas_left);
        
    }];

    self.middleZHUSHULabel.text = [NSString stringWithFormat:@"共%d注%d元",0,0];
    self.yuELabel.text = [NSString stringWithFormat:@"余额：%.3f元",[self.dataSourceModel.balance floatValue]];
    [payBtn addTarget:self action:@selector(payBtnClick) forControlEvents:UIControlEventTouchDown];

}

#pragma mark-setDefaultValue
- (void)setDefaultValue{
    self.startTF.text = @"1";
    self.zhuiTF.text = @"10";//
    self.geFanBeiTF.text = @"1";
    self.zhuiFanBeiTF.text = @"10";//
    self.beiFanBeiTF.text = @"2";
    self.startFanBeiTF.text = @"1";
    self.liRunStartTF.text = @"1";
    self.liRunZhuiTF.text = @"10";//
    self.liRunTF.text = @"50";
    [self tongBeiPlanBtnClick];
}


- (void)reloadDataAndShowWithNum:(int) num andTableView:(UITableView *)tableview{
 
    MCZhuiHaoModel *model = [[MCZhuiHaoModel alloc] init];
    model.LotteryCode = [self.dataSourceModel.LotteryID intValue];
    model.Num = num;
    self.chaseIssuesModel = model;
    [model refreashDataAndShow];
    CGFloat singlePrice = 0;
    singlePrice=[_dataSourceModel.payMoney doubleValue];
    
    model.callBackSuccessBlock = ^(id manager) {
        /*
         *  和数据库进行比较
         */
      
        NSArray *arr = [MCZhuiHaoModel mj_objectArrayWithKeyValuesArray:manager];
        NSInteger count=0;
        if (arr.count>=self.selectedCheckBoxArray.count) {
            count=self.selectedCheckBoxArray.count;
        } else {
            count=arr.count;
            [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"当前最多只能追%ld期！",(long)count]];
            self.zhuiTF.text = [NSString stringWithFormat:@"%ld",(long)count];
        }
        NSMutableArray * marr=[[NSMutableArray alloc]init];
        for (NSInteger i = 0; i<count; i++) {
            MCZhuiHaoModel *model = self.selectedCheckBoxArray[i];
            MCZhuiHaoModel *model1 = arr[i];
            model.typeValue = singlePrice;
//            model.selected = YES;
            model.IssueNumber = model1.IssueNumber;
            [marr addObject:model];
            NSLog(@"%@",model.IssueNumber);
        }
        self.selectedCheckBoxArray=marr;
        [tableview reloadData];
    };
    model.callBackFailedBlock = ^(id manager, NSString *errorCode) {
        
    };
    
}
#pragma mark - touch event

//同倍追号
- (void)tongBeiPlanBtnClick{

    
    [self.view endEditing:YES];
    if ([self.startTF.text intValue]>=9999) {
        self.startTF.text = @"9999";
    }
    if ([self.startTF.text intValue]==0) {
        self.startTF.text = @"1";
    }
    if ([self.zhuiTF.text intValue]==0) {
        self.zhuiTF.text = @"1";
    }
    
    int count = [self.zhuiTF.text intValue];
    if (count>=120) {
    
        count = 120;
    
        self.zhuiTF.text = @"120";
    }
    if (count == 0) {
    
        [self.selectedCheckBoxArray removeAllObjects];
    }
    [self.selectedArray removeAllObjects];
    [self.selectedCheckBoxArray removeAllObjects];

    for (NSInteger i = 0; i< count; i++) {
        MCZhuiHaoModel *model = [[MCZhuiHaoModel alloc] init];
        model.title = [NSString stringWithFormat:@"%ld",i];
        model.selected = YES;
         model.beishu = self.startTF.text;
        [self.selectedCheckBoxArray addObject:model];
    }
    [self reloadDataAndShowWithNum:[self.zhuiTF.text intValue] andTableView:self.leftTableView];
   
}
//翻倍追号
- (void)fanBeiPlanBtnClick{

    
    
    if ([self.startFanBeiTF.text intValue]==0) {
        self.startFanBeiTF.text = @"1";
    }
    if ([self.zhuiFanBeiTF.text intValue]==0) {
        self.zhuiFanBeiTF.text = @"1";
    }
    if ([self.geFanBeiTF.text intValue]==0) {
        self.geFanBeiTF.text = @"1";
    }
    if ([self.beiFanBeiTF.text intValue]==0) {
        self.beiFanBeiTF.text = @"1";
    }
    [self.view endEditing:YES];
    if ([self.startFanBeiTF.text intValue]>=9999) {
        self.startFanBeiTF.text = @"9999";
    }
  
    int count = [self.zhuiFanBeiTF.text intValue];
    if (count>=120) {
        count = 120;
        self.zhuiFanBeiTF.text = @"120";
    }
    if (count == 0) {
        [self.selectedCheckBoxArray removeAllObjects];
    }
    [self.selectedArray removeAllObjects];
    [self.selectedCheckBoxArray removeAllObjects];
    
    if ([self.startFanBeiTF.text intValue] * [self.geFanBeiTF.text intValue]  *[self.beiFanBeiTF.text intValue] >9999 ) {
//        [SVProgressHUD showErrorWithStatus:@"倍数不能超过9999"];
//        return;
    }
       int j = 1;
    for (NSInteger i = 0; i< count; i++) {
    

        if (i<[self.geFanBeiTF.text intValue]) {}
        else{
            if (i==[self.geFanBeiTF.text intValue]) {
                j = 1;
            } else {
                if([self.geFanBeiTF.text intValue] !=0){
                    if ((i)%(([self.geFanBeiTF.text intValue ]))== 0) {
                        j*=[self.beiFanBeiTF.text intValue];
                    }
                    if (j* [self.startFanBeiTF.text intValue] * [self.beiFanBeiTF.text intValue] > 9999) {
                        break;
                    }
                }
              
            }
       
        }
        MCZhuiHaoModel *model = [[MCZhuiHaoModel alloc] init];
        model.title = [NSString stringWithFormat:@"%ld",(long)i];
        model.selected = YES;
        [self.selectedCheckBoxArray addObject:model];
    }
    int k = 1;
    for (NSInteger i= 0;i<self.selectedCheckBoxArray.count;i++) {
        MCZhuiHaoModel *model = self.selectedCheckBoxArray[i];
        
        
        if (i<[self.geFanBeiTF.text intValue]) {
            model.beishu = self.startFanBeiTF.text;
            continue;
        }
        else{
            if (i==[self.geFanBeiTF.text intValue]) {
                k = 1;
            } else {
                if ((i)%(([self.geFanBeiTF.text intValue ]))== 0) {
                    k*=[self.beiFanBeiTF.text intValue];
                }
            }
        }
         model.beishu = [NSString stringWithFormat:@"%d",k * ([self.startFanBeiTF.text intValue] * [self.beiFanBeiTF.text intValue])];
        NSLog(@"%@------",model.beishu);
    }
    if (self.selectedCheckBoxArray.count < [self.zhuiFanBeiTF.text intValue]) {
        [SVProgressHUD showErrorWithStatus:@"您设置的利润率过高，无法达到您的预期目标值，请重新修改参数设置"];
        self.middleZHUSHULabel.text =@"共0注0元";
        
    }
   
    [self reloadDataAndShowWithNum:[self.zhuiFanBeiTF.text intValue] andTableView:self.middleTableView];
  
}

//利润率追号
- (void)liRuanPlanBtnClick{
    
    
    
    if ([self.liRunTF.text intValue]==0) {
        self.liRunTF.text = @"1";
    }
    if ([self.liRunZhuiTF.text intValue]==0) {
        self.liRunZhuiTF.text = @"1";
    }
    if ([self.liRunStartTF.text intValue]==0) {
        self.liRunStartTF.text = @"1";
    }
  
    [self.view endEditing:YES];
    if ([self.liRunZhuiTF.text intValue]>=9999) {
        self.liRunZhuiTF.text = @"9999";
    }
    int count = [self.liRunZhuiTF.text intValue];
    if (count>=120) {
        count = 120;
        self.liRunZhuiTF.text = @"120";
    }
    if (count == 0) {
        [self.selectedCheckBoxArray removeAllObjects];
    }
    [self.selectedArray removeAllObjects];
    [self.selectedCheckBoxArray removeAllObjects];

    
    if ([self.liRunStartTF.text intValue]>9999) {
        [SVProgressHUD showErrorWithStatus:@"倍数不能超过9999"];
        return;
    }
    if ([self.liRunTF.text intValue]>99999) {
        [SVProgressHUD showErrorWithStatus:@"利润率不能超过99999"];
        return;
    }
    // 单倍金额 80
    CGFloat nTotalMoney = 0;
   
    MCPaySelectedCellModel * model = self.dataSourceModel.dataSource.firstObject;
   
    float signalMoney = [model.payMoney floatValue] / [model.stakeNumber floatValue];
    for (NSInteger i = 0; i<[self.liRunZhuiTF.text intValue]; i++) {
        CGFloat beiShuValue = ceilf((([self.liRunTF.text floatValue] /100.0 + 1) * nTotalMoney)/( [model.profitChaseAwardAmount floatValue] - (([self.liRunTF.text floatValue] /100.0 + 1) * signalMoney)));
       int max = MAX(beiShuValue, [self.liRunStartTF.text intValue]);
        nTotalMoney += max * 2;

        int shouYiValue = ([model.profitChaseAwardAmount floatValue] * [self.liRunStartTF.text intValue] - [model.payMoney floatValue]/[model.multiple intValue])/([model.payMoney floatValue]/[model.multiple intValue]);
        if (shouYiValue*100<[self.liRunTF.text intValue]) {
            [SVProgressHUD showErrorWithStatus:@"您设置的利润率过高，无法达到您的预期目标值，请重新修改参数设置"];
            self.middleZHUSHULabel.text =@"共0注0元";
            [self.selectedCheckBoxArray removeAllObjects];
            [self.rightTableView reloadData];
            return;
        }
        if (max>9999) {
            [SVProgressHUD showErrorWithStatus:@"您设置的利润率过高，无法达到您的预期目标值，请重新修改参数设置"];
            self.middleZHUSHULabel.text =@"共0注0元";
            break;
        }
        MCZhuiHaoModel *model = [[MCZhuiHaoModel alloc] init];
        model.title = [NSString stringWithFormat:@"%ld",(long)i];
        model.beishu = [NSString stringWithFormat:@"%d",max];
        model.selected = YES;
        [self.selectedCheckBoxArray addObject:model];
        
    }
    [self reloadDataAndShowWithNum:[self.liRunZhuiTF.text intValue] andTableView:self.rightTableView];
 
    
}
- (void)stopBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected == YES) {
        //开启
        self.isStop= YES;
    } else{
        //关闭
        self.isStop= NO;
    }
}
- (void)payBtnClick{
    
 
    for (MCZhuiHaoModel *model in self.selectedCheckBoxArray) {

        if (([model.beishu intValue] * self.singlePrice+0.001) <0.02) {
            [SVProgressHUD showInfoWithStatus:@"每单最少投注0.02元!"];
            return;
        }
    }
     _firstResbanderZhuiHaoView = YES;
    int mode = (int)[[NSUserDefaults standardUserDefaults] objectForKey:MerchantMode];
    
    NSString *maxstr = @"";
    if ((mode & 128) == 128) {//单期功能
        for (MCMaxbonusModel *model in self.boModelArray) {
            if ([model.Sign isEqualToString:@"1"]) {
                //                            continue;
            }
            
            NSString *lotName =  [MCLotteryID getLotteryCategoriesTypeNameByID:self.baseWFmodel.LotteryID];
            if ([lotName isEqualToString:@"ssc"] &&[model.SingleStageCode isEqualToString:@"1001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"esf"] &&[model.SingleStageCode isEqualToString:@"2001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"sd"] &&[model.SingleStageCode isEqualToString:@"3001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"pls"] &&[model.SingleStageCode isEqualToString:@"4001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"plw"] &&[model.SingleStageCode isEqualToString:@"5001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"ssl"] &&[model.SingleStageCode isEqualToString:@"6001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"kl8"] &&[model.SingleStageCode isEqualToString:@"7001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"pks"] &&[model.SingleStageCode isEqualToString:@"8001"]) {
                maxstr = model.MaxAmt;
            }
            else if ([lotName isEqualToString:@"k3"] &&[model.SingleStageCode isEqualToString:@"9001"]) {
                maxstr = model.MaxAmt;
            }else{
                continue ;
            }
        }
    }
    NSString *str=@"";
    if (maxstr.length>1) {
        str=[NSString stringWithFormat:@"该彩种单期最高奖金为%@元\n您共追%lu期，共%@元，请确认！",GetRealSNum(maxstr),(unsigned long)self.selectedCheckBoxArray.count,GetRealFloatNum(self.needMoney)];
    }else{
        str=[NSString stringWithFormat:@"您共追%lu期，共%@元，请确认！",(unsigned long)self.selectedCheckBoxArray.count,GetRealFloatNum(self.needMoney)];
    }

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        
        int mode = (int)[[NSUserDefaults standardUserDefaults] objectForKey:MerchantMode];
        
        if ((mode & 128) == 128) {//单期功能
            for (MCMaxbonusModel *model in self.boModelArray) {
                if ([model.Sign isEqualToString:@"1"]) {
                 continue;
                }
                NSString *maxstr = @"";
                
                NSString *lotName =  [MCLotteryID getLotteryCategoriesTypeNameByID:self.baseWFmodel.LotteryID];
                if ([lotName isEqualToString:@"ssc"] &&[model.SingleStageCode isEqualToString:@"1001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"esf"] &&[model.SingleStageCode isEqualToString:@"2001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"sd"] &&[model.SingleStageCode isEqualToString:@"3001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"pls"] &&[model.SingleStageCode isEqualToString:@"4001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"plw"] &&[model.SingleStageCode isEqualToString:@"5001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"ssl"] &&[model.SingleStageCode isEqualToString:@"6001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"kl8"] &&[model.SingleStageCode isEqualToString:@"7001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"pks"] &&[model.SingleStageCode isEqualToString:@"8001"]) {
                    maxstr = model.MaxAmt;
                }
                else if ([lotName isEqualToString:@"k3"] &&[model.SingleStageCode isEqualToString:@"9001"]) {
                    maxstr = model.MaxAmt;
                }else{
                   // return ;
                }
                
                NSString *strinfo = [NSString stringWithFormat:@"该彩种单期最高奖金为%@元",maxstr];
                [SVProgressHUD showInfoWithStatus:strinfo];

            }
        }

        NSMutableDictionary *issueList = [NSMutableDictionary dictionary];
        for (MCZhuiHaoModel *model in self.selectedArray) {
       
            [issueList setObject:model.beishu forKey:model.IssueNumber];
        }
        if (self.selectedArray.count>0) {
            MCZhuiHaoModel *model=self.selectedArray[0];
            self.IssueNumber=model.IssueNumber;
        }
        NSMutableArray *arr = [NSMutableArray array];
        for (MCPaySelectedCellModel *modelTemp in self.dataSourceModel.dataSource) {
            if (modelTemp.payMoney != nil&&modelTemp.haoMa != nil&&modelTemp.stakeNumber != nil&&modelTemp.PlayCode != nil&&modelTemp.IssueNumber != nil&&modelTemp.BetRebate != nil&&modelTemp.multiple != nil&&modelTemp.BetMode != nil) {

                NSString * BetMode=@"";
                if (self.isStop) {
                    //开启
                    BetMode= [NSString stringWithFormat:@"%d",([modelTemp.BetMode intValue]|2)];
                    
                    //关闭
                } else{
                    BetMode= [NSString stringWithFormat:@"%d",([self.baseWFmodel.BetMode intValue]|4)];
                    
                }
                NSDictionary *dic =  @{
                                       @"BetMoney":modelTemp.payMoney,
                                       @"BetContent":modelTemp.tz_haoMa,
                                       @"BetCount":modelTemp.stakeNumber,
                                       @"PlayCode":modelTemp.PlayCode,
                                       @"IssueNumber":self.IssueNumber,
                                       @"BetRebate":modelTemp.BetRebate,
                                       @"BetMultiple":modelTemp.multiple,
                                       @"BetMode":BetMode
                                       };
                
                NSLog(@"%@",modelTemp.BetMode);
                [arr addObject:dic];
            }
  
        }
        
        NSDictionary *dicP = @{
                               @"LotteryCode":self.dataSourceModel.LotteryID,
                               @"Bet":arr,
                               @"IssueNumber":self.IssueNumber
                               };
        
       NSDictionary *dic = @{
                                  @"IssueList":issueList,
                                  @"UserBetInfo":dicP
                                  };
        MCBetModel *betModel = [[MCBetModel alloc] initWithDic:dic];
        self.betModel = betModel;
        [betModel refreashDataAndShow];
        
        __weak typeof(self) weakSelf = self;
        betModel.callBackSuccessBlock = ^(ApiBaseManager* manager) {
            
            /*
             * 追号成功   清空购彩蓝
             */
            MCPaySLBaseModel * dataSourceModel=[MCPaySLBaseModel sharedMCPaySLBaseModel];
            [dataSourceModel removeDataSource];

            weakSelf.dataSourceModel.balance = [NSString stringWithFormat:@"%.3f",[weakSelf.dataSourceModel.balance floatValue]- weakSelf.needMoney];
            weakSelf.yuELabel.text = [NSString stringWithFormat:@"余额：%.3f元",[weakSelf.dataSourceModel.balance floatValue]];
            MCUserMoneyModel * userMoneyModel=[MCUserMoneyModel sharedMCUserMoneyModel];
            userMoneyModel.isNeedLoad=YES;
             UIAlertController *alertSuc = [UIAlertController alertControllerWithTitle:@"提示" message:@"投注成功" preferredStyle:UIAlertControllerStyleAlert];
            
            [alertSuc addAction:[UIAlertAction actionWithTitle:@"继续投注" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                NSArray *currentControllers = self.navigationController.viewControllers;
                NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];
                int count = (int)newControllers.count;
                [newControllers removeObjectAtIndex:(count-1)];//3
                [newControllers removeObjectAtIndex:(count-2)];//2

                [self.navigationController setViewControllers:newControllers animated:YES];

                
            }]];
            
            [alertSuc addAction:[UIAlertAction actionWithTitle:@"查看记录" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                MCGameRecordViewController * vc1 = [[MCGameRecordViewController alloc]init];
                [self.navigationController pushViewController:vc1 animated:YES];
            }]];
            
             [weakSelf presentViewController:alertSuc animated:YES completion:nil];
            
            
        };
        NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    [self presentViewController:alert animated:YES completion:nil];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];

}
#pragma mark - registerNotifacation
- (void)registerNotifacation{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)noti{
    NSDictionary *dic = noti.userInfo;
    CGRect rect = [dic[@"UIKeyboardBoundsUserInfoKey"] CGRectValue];
    [UIView animateWithDuration:0.25 animations:^{
        if (self.baseScrollView.contentOffset.x == 0) {
            self.leftTableView.contentOffset = CGPointMake(0, self.currentRect.origin.y);
            self.leftTableView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height, 0);
        } else if(self.baseScrollView.contentOffset.x == G_SCREENWIDTH){
            self.middleTableView.contentOffset = CGPointMake(0, self.currentRect.origin.y);
            self.middleTableView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height, 0);
        }else{
            self.rightTableView.contentOffset = CGPointMake(0, self.currentRect.origin.y);
            self.rightTableView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height, 0);
        }

    }];
    
    
}

- (void)keyBoardWillHide:(NSNotification *)noti{
    
    self.leftTableView.contentOffset = CGPointMake(0, 0);
    self.leftTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - UITableviewDelegate And DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.selectedCheckBoxArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MCZhuihaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhuihaocell"];

    cell.textFiledDidBecomeFisrtR = ^(NSString *str,CGRect rect){
    
        self.currentRect = rect;
    
    };
    
    cell.textFiledDidSelected = ^(MCZhuiHaoModel *model,NSString *lastTimeCount){
      
  
        if (model.selected == YES && ![self.selectedArray containsObject:model]) {
            [self.selectedArray addObject:model];
        } else if(model.selected == NO && [self.selectedArray containsObject:model]){
            [self.selectedArray removeObject:model];
        }
        int count = 0;
        for (MCZhuiHaoModel *model in self.selectedArray) {
            if (model.selected == YES) {
                count += [model.beishu intValue];
            }
            
        }
        
        CGFloat singlePrice = 0.0;
        singlePrice= [_dataSourceModel.payMoney doubleValue];
        //底部金额
        self.middleZHUSHULabel.text = [NSString stringWithFormat:@"共%@注%@元",self.dataSourceModel.stakeNumber,GetRealFloatNum(count * singlePrice)];
        //追号期数
        self.zhuiTF.text = [NSString stringWithFormat:@"%ld",(long)self.selectedArray.count];
        self.zhuiFanBeiTF.text = [NSString stringWithFormat:@"%ld",(long)self.selectedArray.count];
        self.singlePrice = singlePrice;
        self.needMoney=count * singlePrice;
        self.yuELabel.text = [NSString stringWithFormat:@"余额：%.3f元",[self.dataSourceModel.balance floatValue] ];
    };
     NSString *titleStr = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
     cell.title = titleStr;
    if (self.selectedCheckBoxArray.count != 0 && indexPath.row < self.selectedCheckBoxArray.count) {
         cell.dataSource = self.selectedCheckBoxArray[indexPath.row];
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
    
}

#pragma mark - textFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{

  
}





#pragma mark - getter and setter 

- (NSMutableArray *)selectedCheckBoxArray{
    if (_selectedCheckBoxArray == nil) {
        _selectedCheckBoxArray = [NSMutableArray array];
    }
    
    return _selectedCheckBoxArray;
}


- (NSMutableArray *)selectedArray{
    if (_selectedArray == nil) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}





@end
